package com.abel;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.*;
import java.io.IOException;

/**
 * Hello world!
 *
 */
public class App{

    private JestClient jestClient;

    public static void main( String[] args ) throws IOException {
        App app = new App();
        app.initClient();
        //app.testCreateIndex();
        app.testQuery();
    }

    public void initClient(){

        // Construct a new Jest client according to configuration via factory
        JestClientFactory factory;
        factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder("http://localhost:9200")
                .multiThreaded(true)
                //Per default this implementation will create no more than 2 concurrent connections per given route
                .defaultMaxTotalConnectionPerRoute(1)
                // and no more 20 connections in total
                .maxTotalConnection(2)
                .build());
        jestClient = factory.getObject();
    }

    public void testCreateIndex() throws IOException {
        String source = "{\"user\":\"kimchy\"}";
        Index index = new Index.Builder(source).index("twitter").type("tweet").id("1").build();
        JestResult result = jestClient.execute(index);
        System.out.println(result.toString());

    }

    public void testQuery() throws IOException {
        Get get = new Get.Builder("twitter", "1").type("tweet").build();

        JestResult result = jestClient.execute(get);
        System.out.println(result.toString());

    }


}
